package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import util.DateUtil;

/**
 * JDBCDemo
 * 
 * @author Administrator
 * 
 */
public class JDBCTest {

	public static void main(String[] args) {
		JDBCTest jbdcTest;
		try {
			Connection connection = JDBCUtil.getConnection();
			jbdcTest = new JDBCTest();
			// jbdcTest.select(connection);
			// jbdcTest.insert(connection);
			jbdcTest.shiwu(connection);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("获取数据库链接失败");
		}

	}

	public void select(Connection connection) throws SQLException {
		String sql = "select count(*)from user_user";
		PreparedStatement statement = connection.prepareStatement(sql);
		ResultSet rSet = statement.executeQuery();
		while (rSet.next()) {
			System.out.println(rSet.getString(1));
		}
		JDBCUtil.release(rSet, connection, statement);
	}

	public void insert(Connection connection) throws SQLException {
		String sql = "insert user_user (id,name) values(?,?)";
		PreparedStatement statement = connection.prepareStatement(sql);
		statement.setInt(1, 3);
		statement.setString(2, "小明");
		int count = statement.executeUpdate();
		System.out.println(count);
		JDBCUtil.release(null, connection, statement);
	}

	public void update(Connection connection) throws SQLException {
		String sql = "update user_user set name = ? where id = ?";
		PreparedStatement statement = connection.prepareStatement(sql);
		statement.setString(1, "小白");
		statement.setInt(2, 3);
		int count = statement.executeUpdate();
		System.out.println(count);
		JDBCUtil.release(null, connection, statement);
	}

	public int createDatabase(Connection connection) throws SQLException {
		String sql = "create database if not exists cloudflight";
		PreparedStatement statement = connection.prepareStatement(sql);
		return statement.executeUpdate();
	}

	public int createTable(Connection connection) throws SQLException{
		String sql ="create table user_user(id int not null,name varchar(15));";
		PreparedStatement statement = connection.prepareStatement(sql);
		return statement.executeUpdate();
	}

	public void shiwu(Connection connection) {
		Statement statement = null;
		try {
			connection.setAutoCommit(false);
			statement = connection.createStatement();
			/*statement.addBatch("insert user_user (id,name) values(4,'asd')");
			statement.addBatch("insert user_user (id,name) values(5,'asd')");*/
			for(int i=1;i<=100;i++){
				statement.addBatch("insert learn_message_board_(username,content,createdate) values('测试"+i+"','内容"+"','"+DateUtil.getStringFromNowDate()+"')");
			}
			statement.executeBatch();
			connection.commit();
			connection.setAutoCommit(true);
		} catch (SQLException e) {
			if (connection != null) {
				try {
					e.printStackTrace();
					System.out.println("rollback");
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		} finally {
			JDBCUtil.release(null, connection, statement);
		}

	}
}
